<template>
    <div class="e-form">
        <BillTop title="采购计划新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.planBuy" label-width="170px" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="计划日期：" prop="billDate">
                            <el-date-picker
                                v-model="formData.planBuy.billDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="采购人：" prop="buyer">
                            <el-input readonly v-model="formData.planBuy.buyer" placeholder="选择人员">
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectBuyer"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="币种：" prop="currencyId">
                           <el-select
                                v-model="formData.planBuy.currencyId"
                                placeholder="请选择"
                                @change="currencyChange"
                            >
                                 <el-option
                                    v-for="(
                                        item
                                    ) in currencyForm"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="费用合计（元）：">
                            <span>{{ formData.planBuy.purchaseCost }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="变更期数：">
                            <span>{{ formData.planBuy.number }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                            <span>{{ userInfo.orgInfo.orgName }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="填报人：">
                            <span>{{ formData.planBuy.recorder }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                    <el-col :span="24">
                        <el-form-item label="状态：">
                             <span>{{ statusLable(formData.planBuy.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item  label="备注：">
                        <el-input type="textarea" v-model="formData.planBuy.remarks"></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="本位币：" >
                            <span>{{ formData.planBuy.baseCurName }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="人民币汇率：">
                            <span>{{formData.planBuy.rmbRate}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="本位币汇率：" prop="baseCurRate">
                            <el-input
                                v-model="formData.planBuy.baseCurRate"
                                v-enter-float2="{
                                    set: formData,
                                    val: 'planBuy',
                                    key: 'baseCurRate'
                                }"
                                placeholder="请输入内容"
                               :disabled="!showBaseCurName"
                                clearable>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose" v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/equipPlan.js'
import { mapState } from 'vuex'
import requestAudit from '@/api/equip/common.js'
import { selectUserBelowOrg } from '@/api/equip/popupCallback.js'
export default {
    data () {
        return {
            formData: {
                //基础信息
                'planBuy': {
                    'auditTime': '',
                    'auditor': '',
                    'auditorId': '',
                    'baseCurId': '',
                    'baseCurName': '',
                    'baseCurPurchaseCost': 0,
                    'baseCurRate': null,
                    'billDate': '',
                    'billId': '',
                    'billNo': '',
                    'buyer': '',
                    'buyerId': '',
                    'changeBillDate': '',
                    'changeBillId': '',
                    'changeBillNo': '',
                    'changePurchaseCost': 0,
                    'changeState': null,
                    'currency': '',
                    'currencyId': '',
                    'founderId': '',
                    'founderName': '',
                    'modifier': '',
                    'modifierId': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDescription': '',
                    'nullifyReason': '',
                    'number': 0,
                    'orgId': '',
                    'orgName': '',
                    'purchaseCost': 0,
                    'recorder': '',
                    'recorderId': '',
                    'remarks': '',
                    'rmbId': '',
                    'rmbName': '',
                    'rmbRate': 0,
                    'rmbSumPurchaseCost': 0,
                    'state': 0,
                    'version': 0,
                    'workId': '',
                    'yearBudgetPlanId': '',
                    'yearBudgetPlanNo': ''
                }
            },
            rules: {
                billDate: [
                    { required: true, message: '请选择日期', trigger: 'blur' },
                ],
                currencyId: [
                    { required: true, message: '请选择币种', trigger: 'change'  },
                ],
                buyer: [
                    { required: true, message: '请选择采购人', trigger: 'change' },
                ],
                baseCurRate: [
                    { required: true, message: '请输入本位币汇率', trigger: 'change' },
                    { validator: (rule, value, callback)=>{
                        if(value == 0) {
                            callback(new Error('本位币汇率必须大于0'))
                        }else{
                            callback()
                        }
                    }, trigger: 'change' }
                ]
            },
            showBaseCurName: false,
            budgetYearList: [], //预算年度
        }
    },
    components: {
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            currencyForm: state => state.equip.equipData.currencyForm, //币种
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return (obj ?  obj.label :  '')
            }
        },
    },
    created () {
        this.getInfo()
    },
    mounted () {
    },
    methods: {
        getInfo () { //默认获取数据
            const date = new Date()
            this.formData.planBuy.billDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
            const { baseCyByOrgId } = this.userInfo
            this.formData.planBuy.baseCurName = baseCyByOrgId.currencyName  //本位币
            this.formData.planBuy.baseCurId = baseCyByOrgId.currency
            this.formData.planBuy.recorder = localStorage.getItem('userName')
            this.formData.planBuy.buyer = localStorage.getItem('userName')
            this.formData.planBuy.recorderId = localStorage.getItem('userId')
        },
        /**选择采购人（选择本机构及下级机构组织人员） */
        selectBuyer () {
            let obj = {
                title: '选择采购人'
            }
            selectUserBelowOrg(obj).then(res=>{
                this.formData.planBuy.buyer = res.userName
                this.formData.planBuy.buyerId = res.userId
            })
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if (valid) {
                    request.purchasePlanAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/housePurchasePlanEdit',
                                query: {
                                    billid: res,
                                    name: 'changeDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                } else {
                    return false
                }
            })

        },
        //币种切换
        currencyChange (val) {
            this.formData.planBuy.currency = this.currencyForm.find(x=>x.value === val).label  //币种
            requestAudit.getRmbRate({  //获取机构下某币种的人民币汇率
                currency: val,
                orgId: this.userInfo.orgInfo.orgId
            }).then(res=>{
                if(res.rate === 1) {
                    this.formData.planBuy.rmbId = res.currency
                    this.formData.planBuy.rmbRate = res.rate
                }else{
                    this.formData.planBuy.rmbId = res.rateId
                    this.formData.planBuy.rmbRate = res.rate
                }
            }).catch(()=>{

            })
            if(this.formData.planBuy.currencyId === this.formData.planBuy.baseCurId) {   //币种=本位币
                this.formData.planBuy.baseCurRate = 1
                this.showBaseCurName = false
            }else{
                this.formData.planBuy.baseCurRate = null
                this.showBaseCurName = true
            }
        },
        //取消
        handleClose () {
            this.$router.replace('/housePurchasePlanList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
